﻿Imports Control_Vuelos_BE
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class UsuarioADO
    Dim MiConexion As New ConexionADO
    Dim cnx As New SqlConnection(MiConexion.GetCnx)
    Dim cmd As New SqlCommand
    Dim scriterio As String = ""

    Public Function pr_ValidarUsuario(ByVal p_coduser As String, ByVal p_pswuser As String) As Boolean
        Try
            Dim cn As New SqlConnection(MiConexion.GetCnx)
            Using cn
                Dim cmd As New SqlCommand("pr_validarUsuario", cn)
                Using cmd
                    cn.Open()
                    cmd.CommandType = Data.CommandType.StoredProcedure
                    cmd.Parameters.Add("@pscodusr", SqlDbType.VarChar, 20)
                    cmd.Parameters.Add("@pspswusr", SqlDbType.VarChar, 20)
                    cmd.Parameters("@pscodusr").Value = Trim(p_coduser)
                    cmd.Parameters("@pspswusr").Value = Trim(p_pswuser)
                    cmd.Parameters.Add("@pnretval", SqlDbType.Int, 8)
                    cmd.Parameters("@pnretval").Direction = ParameterDirection.Output
                    cmd.ExecuteScalar()
                    If cmd.Parameters("@pnretval").Value = 200 Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch myException As Exception
            MsgBox("Usuario (pr_ValidarUsuario) --> " & myException.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function

    Public Function pr_Select_Usuarios(ByRef p_key As String) As DataTable
        Try
            Dim sql As String = "SELECT nomuser,coduser FROM dbo.Usuario "
            scriterio = IIf(p_key.Trim() = "", "", " Where  " & Trim(p_key))
            Dim cn As New SqlConnection(MiConexion.GetCnx)
            Using cn
                Dim da As SqlDataAdapter = New SqlDataAdapter(sql & scriterio, cn)
                Using da
                    cn.Open()
                    Dim dt As DataTable = New DataTable("User")
                    ' Rellenamos el objeto DataTable
                    da.Fill(dt)
                    Return dt
                End Using
            End Using

        Catch myException As Exception
            MsgBox("Usuario (pr_Select_Usuarios) --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Function pr_Get_Usuario(ByRef p_coduser As String) As Usuario
        Try
            Dim sql As String = "SELECT * FROM dbo.Usuario WITH (NOLOCK) "
            scriterio = String.Format(" Where  coduser = '{0}'", p_coduser.Trim)
            Dim drCurrent As DataRow
            Dim cn As New SqlConnection(MiConexion.GetCnx)
            Dim rstUsuario As New Usuario
            Using cn
                Dim da As SqlDataAdapter = New SqlDataAdapter(sql & scriterio, cn)
                Using da
                    cn.Open()
                    Dim dt As DataTable = New DataTable("Usuario")
                    ' Rellenamos el objeto DataTable
                    da.Fill(dt)
                    For Each drCurrent In dt.Rows
                        rstUsuario.coduser = drCurrent("coduser").ToString
                        rstUsuario.nomuser = drCurrent("nomuser").ToString
                        rstUsuario.codnive = drCurrent("codnive").ToString
                        rstUsuario.maiuser = drCurrent("maiuser").ToString
                    Next
                    Return rstUsuario
                End Using
            End Using
        Catch myException As Exception
            MsgBox("Usuario (pr_Get_Usuario) --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

End Class
